package org.seckill.web;

import org.seckill.dto.ExposerRsp;
import org.seckill.dto.SeckillExecutionRsp;
import org.seckill.dto.SeckillResult;
import org.seckill.entity.Seckill;
import org.seckill.enums.SeckillStatEnum;
import org.seckill.exception.RepeatKillException;
import org.seckill.exception.SeckillCloseException;
import org.seckill.service.SeckillService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/seckill")
@CrossOrigin
public class SeckillController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SeckillService seckillService;

    @GetMapping("/list")
    public List<Seckill> list() {
        return seckillService.getSeckillList();
    }

    @GetMapping("/{seckillId}/detail")
    public List<Seckill> detail(@PathVariable("seckillId") Long seckillId) {
        if (seckillId == null) {
            return null;
        }
        List<Seckill> seckill = seckillService.getById(seckillId);
        if (seckill == null) {
            return null;
        }
        return seckill;
    }

    @PostMapping(value = "/{seckillId}/exposer", produces = {"application/json;charset=UTF-8"})
    public SeckillResult<ExposerRsp> exposer(@PathVariable("seckillId") Long seckillId) {
        SeckillResult<ExposerRsp> result;
        try {
            ExposerRsp exposer = seckillService.exprotSeckillUrl(seckillId);
            result = new SeckillResult<ExposerRsp>(true, exposer);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result = new SeckillResult<ExposerRsp>(false, e.getMessage());
        }
        return result;
    }

    @PostMapping(value = "/{seckillId}/{md5}/execution", produces = {"application/json;charset=UTF-8"})
    public SeckillResult<SeckillExecutionRsp> execute(@PathVariable("seckillId") Long seckillId,
                                                      @PathVariable("md5") String md5,
                                                      @CookieValue(value = "killPhone") Long phone) {

        // springmvc valid
        if (phone == null) {
            return new SeckillResult<SeckillExecutionRsp>(false, "未注册");
        }
        SeckillResult<SeckillExecutionRsp> resullt;
        try {
            SeckillExecutionRsp executionRsp = seckillService.executeSeckill(seckillId, phone, md5);
            return new SeckillResult<SeckillExecutionRsp>(true, executionRsp);
        } catch (RepeatKillException e) {
            SeckillExecutionRsp executionRsp = new SeckillExecutionRsp(seckillId, SeckillStatEnum.REPEAT_KILL);
            return new SeckillResult<SeckillExecutionRsp>(true, executionRsp);
        } catch (SeckillCloseException e) {
            SeckillExecutionRsp executionRsp = new SeckillExecutionRsp(seckillId, SeckillStatEnum.END);
            return new SeckillResult<SeckillExecutionRsp>(true, executionRsp);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            SeckillExecutionRsp executionRsp = new SeckillExecutionRsp(seckillId, SeckillStatEnum.INNER_ERROR);
            return new SeckillResult<SeckillExecutionRsp>(true, executionRsp);
        }

    }

    @GetMapping( "/time/now")
    public SeckillResult<Long> time() {
        Date now = new Date();
        return new SeckillResult(true, now.getTime());
    }

}

